/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	heyong 
 * 
 * <p>描   述  :   DisasterPreventionServiceImpl.java for com.pengtu.gsj.service.impl.check.yh.roadnet    </p>
 * 
 * <p>最后修改 : $: Aug 28, 2017-4:36:20 PM v 1.0.0	 heyong   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.yh.roadnet;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.yh.ReconstructionDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.base.LabelValue;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.yh.Reconstruction;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.yh.roadnet.DisasterPreventionService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.LabelHelper;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * DisasterPreventionServiceImpl
 * 
 * Aug 28, 2017 4:36:20 PM
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class DisasterPreventionServiceImpl implements DisasterPreventionService{
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	@Autowired
	private ReconstructionDao reconstructionDao;
	
	@Override
	public List<Reconstruction> getDisasterPreventionDatas(QueryFilter queryFilter){
		return reconstructionDao.find(queryFilter);
	}
	
	
	@Override
	public List<Map<String, Object>> count(String startTime, String endTime) {
		List<Map<String, Object>> innerList = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = null;
		List<Reconstruction> reconstructions = reconstructionDao.getCountReconstructions(startTime, endTime);
		for (Reconstruction reconstruction : reconstructions) {
			List<Reconstruction> recons = reconstructionDao.findBy("projectType", reconstruction.getProjectType());
			map = new HashMap<String, Object>();
			map.put("name", LabelHelper.getInstance().buildCodeName(5001, reconstruction.getProjectType()));
			map.put("all", recons.size());
			map.put("finished", reconstruction.getCount());
			map.put("rate", new BigDecimal(Double.parseDouble(String.valueOf(reconstruction.getCount()))*100/recons.size()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
			innerList.add(map);
		}
		return  innerList;
	}
	
	
	@Override
	public List<Map<String, Object>> classifyList(String startTime, String endTime){
		List<Map<String, Object>> yearList = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = null;
		List<LabelValue> labels = LabelHelper.getInstance().buildCodeNames(5001);
		for (LabelValue labelValue : labels) {
			List<Reconstruction> yeaReconstructions = reconstructionDao.getcountReconsGroupByType(labelValue.getValue(), startTime, endTime);
			for (Reconstruction reconstruction : yeaReconstructions) {
				map = new HashMap<String, Object>();
				QueryFilter queryFilter = new QueryFilter();
				queryFilter.addFilter("projectYear", reconstruction.getProjectYear(),MatchType.EQ);
				queryFilter.addFilter("projectType", reconstruction.getProjectType(),MatchType.EQ);
				queryFilter.addFilter("finished", "1",MatchType.EQ);
				List<Reconstruction> reconstructs = reconstructionDao.find(queryFilter);
				map.put("label", reconstruction.getProjectType());
				map.put("year", reconstruction.getProjectYear());
				map.put("all", reconstruction.getCount());
				map.put("finished", reconstructs.size());
				map.put("spare", reconstruction.getCount()-reconstructs.size());
				map.put("total", reconstruction.getTotalInvestment());
				map.put("subsidy", reconstruction.getSubsidyAmount());
				map.put("selfRaised", reconstruction.getSelfRaised());
				map.put("rate",new BigDecimal(Double.parseDouble(reconstruction.getRate())).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
				map.put("finishRate",new BigDecimal(Double.parseDouble(String.valueOf(reconstructs.size()))*100/reconstruction.getCount()).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
				yearList.add(map);
			}
		}
		return yearList;
	}
	
	
	/**
	 * 安保灾害危桥防治判分和导出
	 */
	@Override
	public Map<String, Object> getDisasterPreventionMap(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<Reconstruction> abList = new ArrayList<Reconstruction>();
		List<Reconstruction> wqList = new ArrayList<Reconstruction>();
		List<Reconstruction> zhList = new ArrayList<Reconstruction>();
		List<Map<String, Object>> innerList = new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> yearList = new ArrayList<Map<String,Object>>();
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		//每类项目100%完成计划的得分
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		//每类项目每降低1个百分点扣分
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		//实地抽查满分
		double reserveMark = Double.parseDouble((String)jsonObject.get("reserveMark"));
		
		String abProjectId = (String)jsonObject.get("abProjectId");
		String wqProjectId = (String)jsonObject.get("wqProjectId");
		String zhProjectId = (String)jsonObject.get("zhProjectId");
		//获取到国省检时间内综合情况
		innerList = this.count(startTime, endTime);
		//获取到国省检时间内各个类型项目的详情
		yearList = this.classifyList(startTime, endTime);
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		
		int years[] = new int[systemManageService.getCheckYear(endTime, startTime, 0)];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(startTime)+i;
		}
		double total = reserveMark; 	//检查得分(默认实地抽查得满分)
		double percentage =0; 			//系统完善度
		double tableFull  = yearFull*3 + reserveMark;	//满分
		//判分
		if (innerList.size() > 0 ) {
			for (int i = 0; i < innerList.size(); i++) {
				double temp = 0;
				if (Double.parseDouble(innerList.get(i).get("rate").toString()) >= 100.00) {
					total += yearFull;
					temp = yearFull;
				} else {
					total += yearFull <= 100 - Double.parseDouble(innerList.get(i).get("rate").toString())*yearPart ? 0 : (yearFull - (100 - Double.parseDouble(innerList.get(i).get("rate").toString()))*yearPart);
					temp = yearFull <= 100 - Double.parseDouble(innerList.get(i).get("rate").toString())*yearPart ? 0 : (yearFull - (100 - Double.parseDouble(innerList.get(i).get("rate").toString()))*yearPart);
				}
				innerList.get(i).put("mark", MathsUtils.formatDouble(temp, 2));
			}
		}
		boolean flag = false;
		if (StringUtils.isNotEmpty(abProjectId)) {
			QueryFilter queryFilter = new QueryFilter();
			queryFilter.addFilter("projectYear", startTime, MatchType.GE);
			queryFilter.addFilter("projectYear", endTime, MatchType.LE);
			queryFilter.addFilter("id", abProjectId, MatchType.EQ);
			abList = this.getDisasterPreventionDatas(queryFilter);
			flag = true;
		} 
		
		if (StringUtils.isNotEmpty(wqProjectId)) {
			QueryFilter queryFilter = new QueryFilter();
			queryFilter.addFilter("projectYear", startTime, MatchType.GE);
			queryFilter.addFilter("projectYear", endTime, MatchType.LE);
			queryFilter.addFilter("id", wqProjectId, MatchType.EQ);
			wqList= this.getDisasterPreventionDatas(queryFilter);
			flag = true;
		} 
		
		if (StringUtils.isNotEmpty(zhProjectId)) {
			QueryFilter queryFilter = new QueryFilter();
			queryFilter.addFilter("projectYear", startTime, MatchType.GE);
			queryFilter.addFilter("projectYear", endTime, MatchType.LE);
			queryFilter.addFilter("id", zhProjectId, MatchType.EQ);
			zhList = this.getDisasterPreventionDatas(queryFilter);
			flag = true;
		} 
		
		map.put("sum",  MathsUtils.formatDouble(total,2));
		//判断完善度
		percentage = MathsUtils.divDouble(MathsUtils.formatDouble(total,2) *100 , tableFull, 2) ;
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("innerList", innerList);
		map.put("yearList", yearList);
		map.put("percent", percentage);
		map.put("att", list);
		map.put("abList", abList);
		map.put("wqList", wqList);
		map.put("zhList", zhList);
		map.put("flag", flag);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
